Skip to content

perf: optimize streaming SSR buffer management#15620

Closed
ascorbic wants to merge 1 commit into
mainfrom
perf/render-buffer-optimization
Closed

perf: optimize streaming SSR buffer management#15620
ascorbic wants to merge 1 commit into
mainfrom
perf/render-buffer-optimization

Conversation

@ascorbic
Copy link
Copy Markdown
Contributor

Summary

Optimizes buffer management in renderToAsyncIterable for streaming SSR. This is a follow-up to #15605 which focused on the render loop itself — this PR targets the buffer/encoding layer.

What changed

The write() method in renderToAsyncIterable now accumulates string chunks into a currentString variable during the write phase, only encoding to Uint8Array when a non-string chunk arrives or when next() is called. This replaces the previous approach of pushing mixed string | Uint8Array into the buffer and then rescanning + merging in next().

Before: next() iterated the buffer to find consecutive strings, concatenated them, encoded them, then did a second pass to merge all Uint8Arrays. This was O(N²) in the number of string chunks due to the rescan.

After: Strings are concatenated during write() (O(1) per call), and next() sees a buffer of only Uint8Array entries — just measure total length and copy. One TextEncoder.encode() call per flush instead of one per string-group.

Benchmark results (vs main)

Benchmark Change
.md streaming +17%
large-array streaming +14.2%
many-components streaming +6.3%
many-expressions streaming +3.7%
.astro streaming +3%

Non-streaming paths are unaffected (they don't use renderToAsyncIterable).

What was tested and discarded

Three other optimizations were benchmarked individually and in combination during development:

  1. Lazy Promise Wrapping — deferred promiseWithResolvers creation in the render loop. Hurt streaming component throughput by ~9%.
  2. Direct Promise Usage in BufferedRenderer — caused regressions.
  3. String Fast-Path in renderChild — caused regressions.

Only the buffer optimization (this PR) showed consistent improvement across all streaming benchmarks.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Feb 23, 2026

🦋 Changeset detected

Latest commit: ac373ca

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions Bot added the pkg: astro Related to the core `astro` package (scope) label Feb 23, 2026
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Feb 23, 2026

Merging this PR will not alter performance

✅ 18 untouched benchmarks


Comparing perf/render-buffer-optimization (ac373ca) with main (35bc814)

Open in CodSpeed

@ascorbic ascorbic closed this Feb 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: astro Related to the core `astro` package (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant